package chapter_7.demo1;

import java.util.stream.Stream;

/**
 * @author: hero生仔
 */
public class Solution {
    public static void main(String[] args) {
        test01();
    }

    private static void test01() {
        long start = System.currentTimeMillis();
        sequentialSumI(100);
        long end = System.currentTimeMillis();
        System.out.println("所花的时间为：" + (end - start) + "s");

        long start1 = System.currentTimeMillis();
        sequentialSumII(100);
        long end1 = System.currentTimeMillis();
        System.out.println("所花的时间为：" + (end1 - start1) + "s");
    }

    public static long sequentialSumI(long n) {
        return Stream.iterate(1L, i -> i + 1)
                .limit(n)
                .reduce(0L, Long::sum);
    }

    public static long sequentialSumII(long n) {
        return Stream.iterate(1L, i -> i + 1)
                .limit(n)
                .parallel()
                .reduce(0L, Long::sum);
    }
}
